package com.sunxueqi.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 工具类
 * @author Sun
 *
 */
@Slf4j
public class ServletUtils {

	/**
	 * 获取request
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		return getRequestAttributes().getRequest();
	}
	
    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }
    
    /**
     * 将相关信息渲染给客户端
     * @param response 渲染对象
     * @param str --要渲染的字符串
     * @return
     */
    public static String renderString(HttpServletResponse response, String str) {
    	try {
			response.setStatus(200);
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(str);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
    	return null;
    }
    
    /**
     * 获取Integer类型的参数
     * @param paraName
     * @return
     */
    public static Integer getParameterToInt(String paraName) {
		return Integer.parseInt(getRequest().getParameter(paraName));
	}
    
    /**
     * 获取String类型的参数
     */
    public static String getParameter(String paraName) {
    	return getRequest().getParameter(paraName);
    }
    
    
}
